<template>
  <nav class="navbar-container">
    <div :class="['nav-main', type+'-layout']">
      <div class="logo" @click="gotoHome">
        <img style="width: 100%;height: 100%" src="../assets/logo.png">
      </div>
      <div class="menu">
        <template v-if="type === 'main'">
          <div class="menu-item" v-for="item in menu" :key="item.title"
               @click="$router.push(item.to)" :class="{'chosen-menu-item': item.page.includes($route.name)}">
            <span>{{item.title}}</span>
          </div>
          <div class="menu-item" @click="gotoEE">
            <span>继续教育</span>
          </div>
        </template>
      </div>
      <div class="user">
        <div @click="gotoSelf" v-if="userInfo">
          <span>个人中心</span>
        </div>
        <div @click="logout" style="margin-left: 20px" v-if="userInfo">
          <span>登出</span>
        </div>
        <div @click="gotoLogin" v-else>
          <span>登录 | 注册</span>
        </div>
      </div>
    </div>
  </nav>
</template>

<script>
import constants from "_u/constants";

export default {
  name: "NavBar",
  props: ['type'],
  data() {
    return {
      userInfo:undefined,
      menu: [
        {
          title: '首页',
          to: {
            name: 'home'
          },
          page: ['home']
        },
        {
          title: '课程',
          to: {
            name: 'course-list'
          },
          page: ['course-list','course-detail']
        },
        {
          title: '纵论',
          to: {
            name: 'home-discussion'
          },
          page: ['home-discussion']
        }
      ]
    }
  },
  mounted() {
    this.userInfo = JSON.parse(window.localStorage.getItem(constants.ACCESS_TOKEN))
    console.log("个人信息",this.userInfo)
  },
  methods: {
    gotoHome() {
      this.$router.push({
        name: 'home'
      })
    },
    gotoLogin() {
      this.$router.push({
        name: 'login'
      })
    },
    gotoSelf() {
      this.$router.push({
        name: 'self-center'
      })
    },
    logout(){
      window.localStorage.setItem(constants.ACCESS_TOKEN,"")
      window.localStorage.setItem(constants.USER_INFO, JSON.stringify(null))
      this.$router.push({
        name: 'login'
      })
    },
    gotoEE() {
      if (this.userInfo == null){
        this.$router.push({
          name: 'login'
        })
      }
      else if(this.userInfo.status !== 2){
        this.$router.push({
          name: 'bind-business',
          query:{
            status:this.userInfo.status
          }
        })
      }else {
        const { href } = this.$router.resolve({
          name: 'ee-self-center',
          params: {
            courseId: this.$route.params.courseId
          }
        });
        window.open(href, '_blank');
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import "../styles/mixins/index";

@mixin chosen() {
  color: $l11-color;
  border-bottom: 3px solid $l11-color;
}
@mixin bg() {
  background: $l11-color;
}
.navbar-container {
  background: #fff;
  border-bottom: 1px solid #d1d1d1;
}
.nav-main {
  display: flex;
  justify-content: space-between;
  line-height: 3.5;
  text-align: center;
}
.logo {
  cursor: pointer;
  margin-top: 5px;
  width: 160px;
  height: 50px;
  @include bg;
}
.menu {
  display: flex;
  flex: 1;
  .menu-item {
    flex: 0 1.5 70px;
    margin-left: 10px;
    flex-shrink: 0;
    cursor: pointer;
    &.chosen-menu-item {
      @include chosen;
    }
    &:hover {

    }
  }

}
.user {
  min-width: 180px;
  > div {
    cursor: pointer;
  }
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
}
</style>
